package com.hboxs.asl.util.sms;

import com.hboxs.asl.util.HttpSender;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by will on 2015/9/11.
 */
public final class ChuangLanSms {
    public static final String KEY_CODE = "code";
    public static final String KEY_MESSAGE = "message";
    private static final String uri = "http://222.73.117.158/msg/index.jsp";// 应用地址
    private static final String account = "vip518_yijing";// 账号
    private static final String pswd = "M5dq3LH2";// 密码
    private static final boolean needstatus = true;// 是否需要状态报告，需要true，不需要false
    private static final String product = null;// 产品ID
    private static final String extno = null;// 扩展码

    public static Map<String, Object> sendSms(String phone, String content) {
        Map<String, Object> map = new HashMap<>();

        map.put(KEY_CODE, new Integer(-1));
        map.put(KEY_MESSAGE, "未知错误");

        try {
            String returnString = HttpSender.batchSend(uri, account, pswd, phone, content, needstatus, product, extno);
            // System.out.println("Result:" + returnString);
            // 发送成功返回结果包含两行数据，我们只需要第一行
            BufferedReader br = new BufferedReader(new StringReader(returnString));
            returnString = br.readLine();
            String[] results = returnString.split(",");
            // System.out.println(Arrays.toString(results));
            String rstmsg;
            int code = Integer.decode(results[1]);
            if (results.length == 2) {
                switch (code) {
                    case 0:
                        rstmsg = "提交成功";
                        break;
                    case 101:
                        rstmsg = "无此用户";
                        break;
                    case 102:
                        rstmsg = "密码错";
                        break;
                    case 103:
                        rstmsg = "提交过快（提交速度超过流速限制";
                        break;
                    case 104:
                        rstmsg = "系统忙（因平台侧原因，暂时无法处理提交的短信）";
                        break;
                    case 105:
                        rstmsg = "敏感短信（短信内容包含敏感词";
                        break;
                    case 106:
                        rstmsg = "消息长度错（>536或<=0）";
                        break;
                    case 107:
                        rstmsg = "包含错误的手机号码";
                        break;
                    case 108:
                        rstmsg = "手机号码个数错（群发>50000或<=0;单发>200或<=0）";
                        break;
                    case 109:
                        rstmsg = "无发送额度（该用户可用短信数已使用完）";
                        break;
                    case 110:
                        rstmsg = "不在发送时间内";
                        break;
                    case 111:
                        rstmsg = "超出该账户当月发送额度限制";
                        break;
                    case 112:
                        rstmsg = "无此产品，用户没有订购该产品";
                        break;
                    case 113:
                        rstmsg = "extno格式错（非数字或者长度不对）";
                        break;
                    case 115:
                        rstmsg = "自动审核驳回";
                        break;
                    case 116:
                        rstmsg = "签名不合法，未带签名（用户必须带签名的前提下）";
                        break;
                    case 117:
                        rstmsg = "IP地址认证错,请求调用的IP地址不是系统登记的IP地址";
                        break;
                    case 118:
                        rstmsg = "用户没有相应的发送权限";
                        break;
                    case 119:
                        rstmsg = "用户已过期";
                        break;
                    case 120:
                        rstmsg = "短信内容不在白名单中";
                        break;
                    default:
                        rstmsg = "未知错误";
                }
                map.put(KEY_CODE, code);
                map.put(KEY_MESSAGE, rstmsg);
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        return map;
    }

}
